home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Tools / idle / PathBrowser.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  4.5 KB  |  160 lines

  1. import os
  2. import sys
  3. import imp
  4. import string
  5. import tkMessageBox
  6.  
  7. from MultiScrolledLists import MultiScrolledLists
  8.  
  9. class PathBrowser(MultiScrolledLists):
  10.     
  11.     def __init__(self, flist):
  12.         self.flist = flist
  13.         MultiScrolledLists.__init__(self, flist.root, 4)
  14.     
  15.     def longtitle(self):
  16.         return "Path Browser"
  17.     
  18.     def width(self, i):
  19.         return 30
  20.     
  21.     def height(self, i):
  22.         return 20
  23.     
  24.     def subtitle(self, i):
  25.         if i == 0:
  26.             return "Path Entries (sys.path)"
  27.         if i-1 >= len(self.path):
  28.             return ""
  29.         if i == 1:
  30.             return self.path[0]
  31.         if i == 2:
  32.             return "Classes in " + self.path[1]
  33.         if i == 3:
  34.             s = self.path[2]
  35.             i = string.find(s, "(")
  36.             if i > 0:
  37.                 s = s[:i]
  38.             return "Methods of " + s
  39.         return ""
  40.     
  41.     def items(self, i):
  42.         if i == 0:
  43.             return sys.path
  44.         if i == 1:
  45.             return self.listmodules()
  46.         if i == 2:
  47.             return self.listclasses()
  48.         if i == 3:
  49.             return self.listmethods()
  50.     
  51.     def listmodules(self):
  52.         dir = self.path[0] or os.curdir
  53.         modules = {}
  54.         suffixes = imp.get_suffixes()
  55.         allnames = os.listdir(dir)
  56.         sorted = []
  57.         for suff, mode, flag in suffixes:
  58.             i = -len(suff)
  59.             for name in allnames:
  60.                 normed_name = os.path.normcase(name)
  61.                 if normed_name[i:] == suff:
  62.                     mod_name = name[:i]
  63.                     if not modules.has_key(mod_name):
  64.                         modules[mod_name] = None
  65.                         sorted.append((normed_name, name))
  66.         sorted.sort()
  67.         names = []
  68.         for nn, name in sorted:
  69.             names.append(name)
  70.         return names
  71.     
  72.     def listclasses(self):
  73.         import pyclbr
  74.         dir = self.path[0]
  75.         file = self.path[1]
  76.         name, ext = os.path.splitext(file)
  77.         if os.path.normcase(ext) != ".py":
  78.             self.top.bell()
  79.             return []
  80.         try:
  81.             self.top.configure(cursor="watch")
  82.             self.top.update_idletasks()
  83.             try:
  84.                 dict = pyclbr.readmodule(name, [dir] + sys.path)
  85.             finally:
  86.                 self.top.configure(cursor="")
  87.         except ImportError, msg:
  88.             tkMessageBox.showerror("Import error", str(msg), parent=root)
  89.             return []
  90.         items = []
  91.         self.classes = {}
  92.         for key, cl in dict.items():
  93.             if cl.module == name:
  94.                 s = key
  95.                 if cl.super:
  96.                     supers = []
  97.                     for sup in cl.super:
  98.                         if type(sup) is type(''):
  99.                             sname = sup
  100.                         else:
  101.                             sname = sup.name
  102.                             if sup.module != cl.module:
  103.                                 sname = "%s.%s" % (sup.module, sname)
  104.                         supers.append(sname)
  105.                     s = s + "(%s)" % string.join(supers, ", ")
  106.                 items.append((cl.lineno, s))
  107.                 self.classes[s] = cl
  108.         items.sort()
  109.         list = []
  110.         for item, s in items:
  111.             list.append(s)
  112.         return list
  113.     
  114.     def listmethods(self):
  115.         try:
  116.             cl = self.classes[self.path[2]]
  117.         except (IndexError, KeyError):
  118.             return []
  119.         items = []
  120.         for name, lineno in cl.methods.items():
  121.             items.append((lineno, name))
  122.         items.sort()
  123.         list = []
  124.         for item, name in items:
  125.             list.append(name)
  126.         return list
  127.     
  128.     def on_double(self, index, i):
  129.         if i == 0:
  130.             return
  131.         if i >= 1:
  132.             dir = self.path[0]
  133.             file = self.path[1]
  134.             name, ext = os.path.splitext(file)
  135.             if os.path.normcase(ext) != ".py":
  136.                 self.top.bell()
  137.                 return
  138.             fullname = os.path.join(dir, file)
  139.             edit = self.flist.open(fullname)
  140.             if i >= 2:
  141.                 classname = self.path[2]
  142.                 try:
  143.                     cl = self.classes[classname]
  144.                 except KeyError:
  145.                     cl = None
  146.                 else:
  147.                     if i == 2:
  148.                         edit.gotoline(cl.lineno)
  149.                     else:
  150.                         methodname = self.path[3]
  151.                         edit.gotoline(cl.methods[methodname])
  152.  
  153.  
  154. def main():
  155.     import PyShell
  156.     PathBrowser(PyShell.flist)
  157.  
  158. if __name__ == "__main__":
  159.     main()
  160.